
#include "Progressbar.h" 
#include <string.h> 

#define Length 101
#define Style '#'

const char* lable = "|/-\\";

//void ForTest()
//{
//    printf("This is for test...\n");
//}


// version 1
//void ProsBar()
//{
//    char bar[Length];
//    memset(bar, '\0', sizeof(bar));
//
//    int cnt = 0;
//    int len = strlen(lable);
//    while (cnt <= 100)
//    {
//        printf("[%-100s][%3d%%][%c]\r", bar, cnt, lable[cnt % len]);
//        fflush(stdout);
//        bar[cnt++] = Style;
//        usleep(20000);
//    }
//
//    printf("\n");
//}

// version 2

void ProsBar(double total, double current)
{
    static char bar[Length] = { 0 };
    // memset(bar, '\0', sizeof(bar));

    static int cnt = 0;
    int len = strlen(lable);
    double rate = (current * 100.00) / total;
    int loop_count = (int)rate;
    while (cnt <= loop_count)
    {
        bar[cnt++] = Style;
        // usleep(10000);
    }
    printf("[%-100s][%.2lf%%][%c]\r", bar + 1, rate, lable[(cnt - 1) % len]);
    fflush(stdout);

    if (cnt >= 100)
    {
        cnt = 0;
        memset(bar, '\0', sizeof(bar));
    }

    // printf("\n");
}
